/**
 * 文 件 名 :FineGrainedAuthorityDaoImpl.java
 * CopyRright (c) 2012-0418:
 * 文件编号：
 * 创 建 人：薛琦
 * 日    期：2012-04-18
 * 修 改 人：
 * 日   期：
 * 描   述：
 * 版 本 号：
 */
package com.tjtt.tdm.system.dao.impl;

import java.math.BigDecimal;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.HibernateException;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.tjtt.tdm.base.HibernateEntityDAO;
import com.tjtt.tdm.exception.DaoException;
import com.tjtt.tdm.system.dao.FineGrainedAuthorityDao;

/**
 * CopyRright (c)2012-0418: <天健通泰科技 >
 * Project: 贵航神鹰实验数据管理系统      
 * Module ID:
 * Comments:
 * 对于角色对应的菜单按钮在数据库的增删改操作 
 * JDK version used: <JDK1.6> 
 * Namespace:  com.tjtt.tdm.system.dao.impl 
 * Author： xueqi
 * Create Date： 2012-04-18 
 * Modified  By： xueqi
 * Modified Date:2012-05-02
 * Why & What is modified <修改原因描述> 为了统一代码，去掉了jdbctemplate，全用HibernateTemplate去做，修改了以下方法public Long findExistData(final Object[]args)，
 *                   	public void delRoleButtonsByRoleId(Long roleId)，	public List getRoleButtons( final Long[] roleids),public List getRoleButtons(final Long roleid) 
 * Version: <版本号>
 */
public class FineGrainedAuthorityDaoImpl extends HibernateEntityDAO implements
		FineGrainedAuthorityDao {
//	private JdbcTemplate jdbcTemplate;

	private String hql = "";

	private List resultList = null;

	

	public List getButtonsByMenuId(Long menuId) throws DaoException {
		resultList = null;
		hql = "from Opratingbutton o where o.menuid=?";
		resultList = this.findByHql(hql, menuId);

		return resultList;
	}

	public List getButtonsByRoleId(Long roleId) throws DaoException {

		hql = "from Opratingbutton o left join Rolemenuoperation r where o.menuid=r.menuid and r.roleid=?";
		resultList = this.findByHql(hql, roleId);

		return resultList;

	}
	public List getButtonsByRoleId(Long[] roleIds) throws DaoException {
		StringBuilder sb=new StringBuilder();
		sb.append("  and ( ");
		for (int i = 0; i < roleIds.length; i++) {
			if(i==0)
			{
			sb.append("   a.roleid=? ");
			}else{
				
				sb.append("   or a.roleid=? ");	
			}
		}
		sb.append("  ) ");
		hql = "from Opratingbutton o left join Rolemenuoperation r where o.menuid=r.menuid "+sb.toString();
		resultList = this.findByHql(hql, roleIds);

		return resultList;

	}
	public void saveRoleButtons(List roleButtonsList) throws DaoException {

		this.saveOrUpdateAll(roleButtonsList);

	}

	public void delRoleButtonsByRoleId(Long roleId) {
		String hql = "from  Rolemenuoperation r where r.roleid=?";
		List tempList = this.getHibernateTemplate().find(hql,roleId);
		//String sql = "DELETE  rolemenuoperation r where r.roleid=?";
		this.getHibernateTemplate().deleteAll(tempList);

	}

	public void saveButtons(List buttonList) throws DaoException {

		this.saveOrUpdateAll(buttonList);

	}

	public void delButtonsByParam(Long menuId, Long buttonId) {
		hql = "from Opratingbutton  o where o.menuid=?  and o.opratingbuttonid=?";
		List tempList = this.getHibernateTemplate().find(hql,new Object[]{menuId,buttonId});

		this.getHibernateTemplate().deleteAll(tempList);

	}
    
	public List getAllMenus() {
      hql="from Menu ";
   resultList=  this.getHibernateTemplate().find(hql);
   return resultList;
	}

	public void delDatas(Collection collection) {

		this.getHibernateTemplate().deleteAll(collection);

	}

	public List getRoleButtons(final Long roleid) {
		
		
		List resultList	=(List)this.getHibernateTemplate().execute(new HibernateCallback()  
		  
	       {  
	  
			String sql = "select b.*, case  when  a.opratingbuttonid is null then 'false'  when a.opratingbuttonid is not  null then 'true' end case "
				+ " from  sys_opratingbutton  b "
				+ " left join sys_rolemenuoperation  a "
				+ " on b.opratingbuttonid = a.opratingbuttonid and a.menueid=b.menuid and    a.roleid=? order by b.menuid ";
	           public Object doInHibernate(Session session)  
	  
	                  throws HibernateException  
	  
	           {  
	        	   SQLQuery query    =session.createSQLQuery(sql); 
	        	   query.setLong(0, roleid);
	              List list =query.list();
	  
	              return list;  
	  
	           }  
	  
	       });  
		
		
	
		  Object [] array=resultList.toArray();
		  resultList.clear();
	        for (int i = 0; i < array.length; i++) {
	        	Object []childArray= (Object [])array[i];
				Map tempMap=new HashMap<String, Object>();
				tempMap.put("opratingbuttonid", childArray[0]);
				tempMap.put("buttonkey", childArray[1]);
				tempMap.put("buttonname", childArray[2]);
				tempMap.put("menuid", childArray[3]);
				tempMap.put("requesturl", childArray[4]);
				tempMap.put("case", childArray[5]);
				resultList.add(tempMap);
			}
			return resultList;
		
	}
	public List getRoleButtons( final Long[] roleids) {
		
		StringBuilder sb=new StringBuilder();
		sb.append("  and ( ");
		for (int i = 0; i < roleids.length; i++) {
			if(i==0)
			{
			sb.append("   a.roleid=? ");
			}else{
				
				sb.append("   or a.roleid=? ");	
			}
		}
		sb.append("  ) ");
	
	List resultList=null;
	resultList=(List)this.getHibernateTemplate().execute(new HibernateCallback()  
		  
	       {  
			String sql = "select b.*, case  when  a.opratingbuttonid is null then 'false'  when a.opratingbuttonid is not  null then 'true' end case "
				+ " from  sys_opratingbutton  b "
				+ " left join sys_rolemenuoperation  a "
				+ " on b.opratingbuttonid = a.opratingbuttonid and a.menueid=b.menuid and    a.roleid=? order by b.menuid ";
	           public Object doInHibernate(Session session)  
	  
	                  throws HibernateException  
	  
	           {  
	        	   SQLQuery query    =session.createSQLQuery(sql); 
	        	  for (int i = 0; i < roleids.length; i++) {
	        	   query.setLong(i, roleids[i]);
	        	  }
	              List list =query.list();
	  
	              return list;  
	  
	           }  
	  
	       });  
	   Object [] array=resultList.toArray();
	   resultList.clear();
        for (int i = 0; i < array.length; i++) {
        	Object []childArray= (Object [])array[i];
			Map tempMap=new HashMap<String, Object>();
			tempMap.put("opratingbuttonid", childArray[0]);
			tempMap.put("buttonkey", childArray[1]);
			tempMap.put("buttonname", childArray[2]);
			tempMap.put("menuid", childArray[3]);
			tempMap.put("requesturl", childArray[4]);
			tempMap.put("case", childArray[5]);
			resultList.add(tempMap);
		}
		return resultList;
	}
	
	
	public Long findExistData(final Object[]args)
	{
		String sql="select count(opratingbuttonid)as countVar from sys_opratingbutton  where menuid=? and buttonkey=? ";
		List resultList=null;
		resultList=(List)this.getHibernateTemplate().execute(new HibernateCallback()  
			  
		       { 
			 public Object doInHibernate(Session session)  
			  
             throws HibernateException  

      {  
		String sql="select count(opratingbuttonid)as countVar from sys_opratingbutton  where menuid=? and buttonkey=? ";
   	   SQLQuery query    =session.createSQLQuery(sql); 
   	  
   	   query.setLong(0, (Long)args[0]);
   	   query.setString(1, (String)args[1]);
         List list =query.list();

         return list;  

      }  
		       });	
//		Object []array=(Object[])resultList.get(0);
//		Object[] childArray=(Object[]) array[0];
		Long	count=((BigDecimal)resultList.get(0)).longValue();
	
		return count;
		
	}
}
